package ysq.xyg.ticket.service.impl;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.github.pagehelper.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;
import ysq.xyg.base.model.dto.ReturnEntity;
import ysq.xyg.base.service.impl.BaseServiceImpl;
import ysq.xyg.base.util.BaseConstant;
import ysq.xyg.sys.model.po.SysDictContent;
import ysq.xyg.ticket.dao.TicketColumnDao;
import ysq.xyg.ticket.model.po.TicketColumn;
import ysq.xyg.sys.model.po.SysUser;
import ysq.xyg.ticket.model.vo.TicketColumnVo;
import ysq.xyg.ticket.service.TicketColumnService;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;


/**
 * 票卷模块-票卷栏目service实现
 *
 * @author shengQiang yu
 * @date 2019-10-24 10:09:13
 */
@Service("ticketColumnService")
public class TicketColumnServiceImpl extends BaseServiceImpl<TicketColumn> implements TicketColumnService {

    @Resource
    private TicketColumnDao ticketColumnDao;

    private TicketColumn ticketColumn;

    @Autowired
    public void setBaseDao(TicketColumnDao ticketColumnDao) {
        this.baseDao = ticketColumnDao;
    }


    /**
     * 分页查询
     *
     * @param ticketColumnVo 对象
     * @param pageSize 展示数量
     * @param pageNum 当前页面
     * @return ReturnEntity 对象
     * @author shengQiang yu
     * @date 2019-10-24 10:09:13
     */
    public ReturnEntity page(TicketColumnVo ticketColumnVo, int pageSize, int pageNum) {
        PageHelper.startPage(pageNum,pageSize);
        PageHelper.orderBy(" sort desc,update_time desc");
        List<TicketColumn> list = acceptList(ticketColumnVo);
        PageInfo<TicketColumn> page=new PageInfo<TicketColumn>(list);
        return new ReturnEntity(page.getList(),page.getTotal(),page.isHasNextPage());
    }

    /**
     * 修改
     *
     * @param ticketColumnVo 对象
     * @param currentUser 当前用户
     * @return ReturnEntity 对象
     * @author shengQiang yu
     * @date 2019-10-24 10:09:13
     */
    public ReturnEntity addOrUpdate(TicketColumnVo ticketColumnVo, SysUser currentUser) {
        boolean isUpate=true;
        if(StrUtil.isEmpty(ticketColumnVo.getId())){
            ticketColumn=new TicketColumn(currentUser);
            isUpate=false;
        }else{
            ticketColumn=selectByPrimaryKey(ticketColumnVo.getId());
            if(ObjectUtil.isNull(ticketColumn)){
                return new ReturnEntity(BaseConstant.FAIL,BaseConstant.DO_FAIL);
            }
            ticketColumn.setUpdateTime(new Date());
            ticketColumn.setUpdateUser(currentUser.getId());
        }
        ticketColumn.setName(ticketColumnVo.getName());
        ticketColumn.setDescription(ticketColumnVo.getDescription());
        ticketColumn.setSort(ticketColumnVo.getSort());
        ticketColumn.setShowType(ticketColumnVo.getShowType());
        int num=0;
        if(isUpate){
            num=updateByPrimaryKey(ticketColumn);
        }else{
            num=insert(ticketColumn);
        }
        if(num<=0){
            return new ReturnEntity(BaseConstant.FAIL,BaseConstant.DO_FAIL);
        }
        return new ReturnEntity();
    }
    /**
     * 删除
     *
     * @param id 主键id
     * @param currentUser 当前用户
     * @return ReturnEntity 对象
     * @author shengQiang yu
     * @date 2019-10-24 10:09:13
     */
    public ReturnEntity delete(String id, SysUser currentUser) {
        TicketColumn ticketColumn=selectByPrimaryKey(id);
        if(ticketColumn!=null){
            ticketColumn.setRemoveTag(BaseConstant.BASE_REMOVE_TAG_DELETE);
            ticketColumn.setUpdateTime(new Date());
            ticketColumn.setUpdateUser(currentUser.getId());
            int num=updateByPrimaryKey(ticketColumn);
            if(num>0){
                return new ReturnEntity();
            }
        }
        return new ReturnEntity(BaseConstant.FAIL,BaseConstant.DO_FAIL);
    }

    /**
     * 获取集合
     *
     * @param ticketColumnVo 对象
     * @return List<BaseDict> BaseDict集合
     * @author shengQiang yu
     * @date 2019-10-24 10:09:13
     */
    public List<TicketColumn> acceptList(TicketColumnVo ticketColumnVo){
        Example example = new Example(TicketColumn.class);
        Example.Criteria criteria = example.createCriteria();
        if(StringUtil.isNotEmpty(ticketColumnVo.getRemoveTag())){
            criteria.andEqualTo("removeTag",ticketColumnVo.getRemoveTag());
        }
        if(StringUtil.isNotEmpty(ticketColumnVo.getShowType())){
            criteria.andEqualTo("showType",ticketColumnVo.getShowType());
        }
        if(StringUtil.isNotEmpty(ticketColumnVo.getName())){
            criteria.andLike("name","%"+ticketColumnVo.getName()+"%");
        }
        example.setOrderByClause(" sort desc,update_time desc");
        return ticketColumnDao.selectByExample(example);
    }
    /**
     * 获取下拉框集合
     *
     * @param code code值
     * @author shengQiang yu
     * @date 2019-4-25 16:49:54
     */
    @Override
    public ReturnEntity acceptSelect(String code) {
        List<TicketColumn> list = acceptList(new TicketColumnVo());
        List<SysDictContent> sysDictContentList=new ArrayList<SysDictContent>();
        for(TicketColumn webColumnType:list){
            SysDictContent sysDictContent=new SysDictContent();
            sysDictContent.setId(webColumnType.getId());
            sysDictContent.setName(webColumnType.getName());
            sysDictContent.setDictType(code);
            sysDictContent.setValue(webColumnType.getId());
            sysDictContentList.add(sysDictContent);
        }
        return new ReturnEntity(sysDictContentList);
    }
}
